package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.classes.ClassesTransfer;
import com.admin.education.dataTransferObject.classes.Request.CreateClassesRequest;
import com.admin.education.dataTransferObject.classes.Request.QueryClassesRequest;
import com.admin.education.dataTransferObject.classes.Request.UpdateClassesRequest;
import com.admin.education.dataTransferObject.classes.mapper.ClassesMapper;
import com.admin.education.dataTransferObject.schools.SchoolsTransfer;
import com.admin.education.dataTransferObject.schools.mapper.SchoolsMapper;
import com.admin.education.models.Classes;
import com.admin.education.models.QClasses;
import com.admin.education.models.Schools;
import com.admin.education.repositories.ClassRepository;
import com.admin.education.services.Interfaces.IClassService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Validated
public class ClassService implements IClassService {

    @Autowired
    private ClassesMapper classesMapper;
    private QClasses qClasses;
    @Autowired
    private ClassRepository classRepository;
    public ClassService(){
        qClasses = QClasses.classes;
    }
    @Override
    public ServicesExcuteResult<PageResponse<ClassesTransfer>> queryClasses(@Valid QueryClassesRequest queryClassesRequest) {

        ServicesExcuteResult<PageResponse<ClassesTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();
        JPAQuery<Classes> classesJPAQuery = this.classRepository.selectFrom(qClasses);
        if(!StringUtils.isEmpty(queryClassesRequest.getName())){
            classesJPAQuery =classesJPAQuery.where(qClasses.name.contains(queryClassesRequest.getName()));
        }
        if(null!= queryClassesRequest.getSchoolsId()){
            classesJPAQuery = classesJPAQuery.where(qClasses.schools.id.eq(queryClassesRequest.getSchoolsId()));
        }
        long totalCount = classesJPAQuery.fetchCount();

        classesJPAQuery = classesJPAQuery.orderBy(qClasses.createDate.desc()).offset((queryClassesRequest.getPageIndex()-1)*queryClassesRequest.getPageSize()).limit(queryClassesRequest.getPageSize());

        List<Classes> classes= classesJPAQuery.fetch();

        List<ClassesTransfer> classesTransfers = classesMapper.mapFrom(classes);

        PageResponse<ClassesTransfer> classesTransferPageResponse = new PageResponse<>(queryClassesRequest.getPageSize(),queryClassesRequest.getPageIndex(),(int)totalCount,classesTransfers);

        servicesExcuteResult.setData(classesTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createClasses(@Valid CreateClassesRequest createClassesRequest) throws BadRequestException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Classes classes = classesMapper.mapFrom(createClassesRequest);
        classes.setCreateDate(new Date());
        classes.setValid(true);

        this.classRepository.persist(classes);

        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateClasses(long classesId, @Valid UpdateClassesRequest updateClassesRequest) throws BadRequestException, ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Classes> classesJPAQuery = this.classRepository.selectFrom(qClasses).where(qClasses.id.eq(classesId));
        if(0!=classesJPAQuery.fetchCount()){
            Classes classes=classesJPAQuery.fetchFirst();

            classes = classesMapper.mapFrom(updateClassesRequest,classes);

            this.classRepository.merge(classes);

            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("班级不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateClassesValid(long classesId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();
        JPAQuery<Classes> classesJPAQuery = this.classRepository.selectFrom(qClasses).where(qClasses.id.eq(classesId));
        if(0!=classesJPAQuery.fetchCount()){
            Classes classes=classesJPAQuery.fetchFirst();

            classes.setValid(!classes.isValid());
            this.classRepository.merge(classes);

            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("班级不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteClasses(long classesId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();
        JPAQuery<Classes> classesJPAQuery = this.classRepository.selectFrom(qClasses).where(qClasses.id.eq(classesId));
        if(0!=classesJPAQuery.fetchCount()){
            Classes classes = classesJPAQuery.fetchFirst();

            if(!classes.isValid()){

                this.classRepository.remove(classes);

                servicesExcuteResult.setSuccess(true);
            }
            else
            {
                servicesExcuteResult.setErrorMessage("班级不能删除");
            }
        }
        else {
            throw new ResourceNotFoundException("班级不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<ClassesTransfer> queryClassesById(long classesId) throws ResourceNotFoundException {
        ServicesExcuteResult<ClassesTransfer> servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Classes> classesJPAQuery = this.classRepository.selectFrom(qClasses).where(qClasses.id.eq(classesId));
        if(0!=classesJPAQuery.fetchCount()){
            Classes classes = classesJPAQuery.fetchFirst();

            servicesExcuteResult.setData(classesMapper.mapFrom(classes));
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("班级不存在");
        }
        return  servicesExcuteResult;
    }
}
